<?php 
/**
* 公共函数库
*
**/
/*
 * 读取系统配置
 */
function C($key,...$args){
    static $settings;
    if(is_null($settings)){
        require BASE_PATH_PUBLIC.'/config/config.php';
    }
    $keys = func_get_args();
    $setting = [];
    foreach($keys as $k=>$v){
        if($k == 0){
            $setting = $settings[$v];
        }elseif($v == end($keys)){
            return $setting[$v];
        }else{
            $setting = $setting[$v];
        }
    }
    return $setting;
}
/*
 * 数据库连接实例,此函数多数用于命令行模式连接数据库
 */
function M()
{
    static $capsule;
    if(is_null($capsule)){
        $capsule = new Illuminate\Database\Capsule\Manager;
        // 创建链接
        $capsule->addConnection(C('settings','db'));
        //$capsule->setEventDispatcher(new Dispatcher(new IlluminateContainer));
        // 设置全局静态可访问DB
        $capsule->setAsGlobal();
        // 启动Eloquent （如果只使用查询构造器，这个可以注释）
        $capsule->bootEloquent();
    }
    return $capsule;
}
/*
 * 重新启动框架
 */
function init()
{
    require_once BASE_PATH.'/index.php';
}

//打印参数,支持多个
function dd($a, ...$c)
{
    $data = func_get_args();
    echo '<pre>';
    if (count($data) == 1) {
        var_dump($data[0]);
        return;
    }
    foreach ($data as $k => $v) {
        print_r($v);
        echo '<br>';
    }
    die;
}

//json函数
function JSON($state=200,$msg='',$data=[])
{
    return json_encode([
        'state'=>$state,
        'msg'=>$msg,
        'data'=>$data?:[]
    ]);
}
/*
 * 判断是否是移动端
 */
function is_mobile(){
//正则表达式,批配不同手机浏览器UA关键词。
    $regex_match = '/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|';
$regex_match .= 'htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|';
$regex_match .= 'blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|';
$regex_match .= 'symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|';
$regex_match .= 'jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320×320|240×320|176×220';
$regex_match .= ')/i';
return isset($_SERVER["HTTP_X_WAP_PROFILE"]) or isset($_SERVER["HTTP_PROFILE"]) or preg_match($regex_match, strtolower($_SERVER["HTTP_USER_AGENT"]));
}
/*
 * 获取ip
 */
function getIp()
{
    if (@$_SERVER['HTTP_CLIENT_IP'] && $_SERVER['HTTP_CLIENT_IP'] != 'unknown') {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (@$_SERVER['HTTP_X_FORWARDED_FOR'] && $_SERVER['HTTP_X_FORWARDED_FOR'] != 'unknown') {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return preg_match('/^\d[\d.]+\d$/', $ip) ? $ip : '';
}
/**
 * 获取 IP  地理位置
 * 淘宝IP接口
 * @Return: array
 */
function getCity($ip = '')
{
    if($ip == ''){
        $url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json";
        $ip=json_decode(file_get_contents($url),true);
        $data = $ip;
    }else{
        $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
        $ip=json_decode(file_get_contents($url));
        if((string)$ip->code=='1'){
            return false;
        }
        $data = (array)$ip->data;
    }
    return $data;
}
/*
 * 返回redis类型
 */
function redisType($type)
{
    $typeArr = [
        \redis::REDIS_NOT_FOUND=>'none',
        \redis::REDIS_STRING=>'string',
        \redis::REDIS_SET=>'set',
        \redis::REDIS_LIST=>'list',
        \redis::REDIS_ZSET=>'zset',
        \redis::REDIS_HASH=>'hash',
    ];
    return $typeArr[$type];
}
/*
 * 发送curl
 *
 */
function curl($url='',$data=[],$post=0)
{
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    if ($post) {
        curl_setopt($ch,CURLOPT_POST,$post);
        curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($data));
    }
    $res = curl_exec($ch);
    curl_close($ch);
    if (!empty($error = curl_error($ch))) {
        $res = $error;
    }
    return $res;
}
